﻿mini.parse();

//要删除对象，表名和ID;对象模式{table:"tablename",id:"id值"};
var _removeData = [];
function fnFormRemove(table,key_field,key_value) {
    var entity = {};
    entity.table = table;
    entity.key_field = key_field;
    entity.key_value = key_value;
    _removeData.push(entity);
}


function fnSave() {
    ///数据检查
    if (typeof (fnCustomDataCheck) != "undefined") {
        var msg = fnCustomDataCheck();
        if (msg != "") {
            alert(msg);
            return;
        }
    }
    ///客制化数据保存
    if (typeof (fnCustomSave) != "undefined") {
        fnCustomSave();
        return;
    }

    var form = new mini.Form("#form1");
    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    fnClone(form_data, instance);
    if (typeof (instance) != "undefined" && instance != null) {
        var msg = mini.loading("数据保存中，请稍后......", "系统消息提醒");
        var json = mini.encode(instance);   //序列化成JSON
        $.ajax({
            url: "/extendpage/action/mdatasave.aspx?entity=" + const_EditEntity + "&flag=1&action=batch",
            type: "post",
            data: { data: json },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    alert("数据保存成功!")
                    fnClose();
                } else {
                    alert(text);
                    //alert("数据保存失败!")
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
        mini.hideMessageBox(msg);
    }
}


//反序列化实体对象到表单
function fnSetForm() {
    var form = new mini.Form("#form1");
    fnClone(instance, instance);
    form.setData(instance, false);
}

//反序列化实体对象到表单
function fnCustomeSetForm(formId, obj) {
    var form = new mini.Form("#"+formId);
    fnClone(obj, obj);
    form.setData(obj, false);
}

///克隆对象
var fnClone = function (sourceObject, targetObject) {
    $.each(sourceObject, function (property, value) {
        if (property != "") {
            targetObject[property] =$.trim(value);
            //eval("targetObject." + property + "='" + value + "'");
        }
    });
};



function fnDataGridDataBing(grid, url) {
    var _grid = mini.get(grid);
    _grid.setUrl(url);
    _grid.reload();
}


var fnClose = function () {
    window.CloseOwnerWindow();
};


$(document).ready(function () {
    $("#btnSave").click(fnSave);
    $("#btnCancel").click(fnClose);
    fnSetForm();
});





//获取URL querystring parameter
function getPar1(source, name) {
    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(source)) return RegExp.$2; return "";
}

function getPar(name) {
    var _url = "http://" + document.location;
    return getPar1(_url, name);
}
